﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Pibby.Controls;

namespace Pibby.UI
{
	public partial class AddPodcastForm : Form
	{
		public AddPodcastForm()
		{
			InitializeComponent();
		}

		private void AddPodcastForm_Load(object sender, EventArgs e)
		{
			this.okButton.Enabled = false;

			if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
			{
				string candidateUrl = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();

				try
				{
					Uri candidate = new Uri(candidateUrl);

					this.urlTextBox.Text = candidateUrl;
				}
				catch
				{
					// don't set anything...
				}
			}

			using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Pibby.Images.podcast.png"))
			{
				this.pictureBox1.Image = Bitmap.FromStream (s);
			}
		
		}

		public string SelectedFeed
		{
			get;
			private set;
		}

		public string SelectedName
		{
			get;
			private set;
		}

		private void okButton_Click(object sender, EventArgs e)
		{
			try
			{
				bool okToClose = false;

				// first try to construct a decent URL out of the text...
				Uri newFeed = new Uri(this.urlTextBox.Text);

				this.SelectedFeed = newFeed.AbsoluteUri;
				this.SelectedName = this.nameTextBox.Text;

				okToClose = !string.IsNullOrEmpty(this.SelectedFeed);

				if (okToClose)
				{
					this.DialogResult = DialogResult.OK;
					this.Close();
				}
			}
			catch (UriFormatException ex)
			{
				//TODO: Put nice message here ...

				ExceptionManager.LogException(ex);

				this.urlTextBox.Focus();
				this.urlTextBox.SelectAll();
			}
		}

		private void urlTextBox_TextChanged(object sender, EventArgs e)
		{
			this.okButton.Enabled = (!string.IsNullOrEmpty(this.urlTextBox.Text));
		}
	}
}
